load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//bazel:copts.bzl", "copts")
load("//bazel:malloc.bzl", "malloc")

cc_library(
    name = "death-constants",
    hdrs = ["death-constants.h"],
)

cc_binary(
    name = "death-client",
    srcs = [
        "death-client.cc",
    ],
    copts = copts(),
    # Use a faster implementation of malloc (and show that tests pass with it).
    malloc = malloc(),
    deps = [
        ":death-constants",
        "//eventuals/grpc",
        "@com_github_grpc_grpc//examples/protos:helloworld_cc_grpc",
    ] + select(
        {
            # We support backward lib only for Linux.
            # The support for Windows and MacOS might be added in future.
            # https://github.com/3rdparty/eventuals/issues/450
            # https://github.com/3rdparty/eventuals/issues/616
            "@platforms//os:linux": [
                "@com_github_3rdparty_bazel_rules_backward_cpp_stacktrace//:backward-stacktrace",
            ],
            "//conditions:default": [],
        },
    ),
)

cc_binary(
    name = "death-server",
    srcs = [
        "death-server.cc",
    ],
    copts = copts(),
    # Use a faster implementation of malloc (and show that tests pass with it).
    malloc = malloc(),
    deps = [
        ":death-constants",
        "//eventuals/grpc",
        "@com_github_grpc_grpc//examples/protos:helloworld_cc_grpc",
    ] + select(
        {
            # We support backward lib only for Linux.
            # The support for Windows and MacOS might be added in future.
            # https://github.com/3rdparty/eventuals/issues/450
            # https://github.com/3rdparty/eventuals/issues/616
            "@platforms//os:linux": [
                "@com_github_3rdparty_bazel_rules_backward_cpp_stacktrace//:backward-stacktrace",
            ],
            "//conditions:default": [],
        },
    ),
)

cc_library(
    name = "streaming",
    srcs = [
        "streaming/streaming.cc",
        "streaming/streaming.h",
        "test.h",
    ],
    copts = copts(),
    deps = [
        "//eventuals/grpc",
        "//test:promisify-for-test",
        "@com_github_google_googletest//:gtest",
        "@com_github_grpc_grpc//examples/protos:keyvaluestore",
    ] + select(
        {
            # We support backward lib only for Linux.
            # The support for Windows and MacOS might be added in future.
            # https://github.com/3rdparty/eventuals/issues/450
            # https://github.com/3rdparty/eventuals/issues/616
            "@platforms//os:linux": [
                "@com_github_3rdparty_bazel_rules_backward_cpp_stacktrace//:backward-stacktrace",
            ],
            "//conditions:default": [],
        },
    ),
)

cc_test(
    name = "grpc",
    timeout = "short",
    # TODO(alexmc): Some of these filenames end with "-test" and others don't.
    # Rename things for consistency.
    srcs = [
        "accept.cc",
        "build-and-start.cc",
        "cancelled-by-client.cc",
        "cancelled-by-client-no-finish.cc",
        "cancelled-by-server.cc",
        "client-death-test.cc",
        "client-interruptible.cc",
        "deadline.cc",
        "greeter-server.cc",
        "helloworld.eventuals.cc",
        "helloworld.eventuals.h",
        "main.cc",
        "multiple-hosts.cc",
        "server-death-test.cc",
        "server-unavailable.cc",
        "streaming/write_last_after_reply_two_requests.cc",
        "streaming/write_last_before_reply_one_request.cc",
        "streaming/write_last_before_reply_two_requests.cc",
        "streaming/writes_done_after_reply_one_request.cc",
        "streaming/writes_done_after_reply_two_requests.cc",
        "streaming/writes_done_before_reply_one_request.cc",
        "streaming/writes_done_before_reply_two_requests.cc",
        "test.h",
        "unary.cc",
        "unimplemented.cc",
    ],
    copts = copts(),
    data = [
        ":death-client",
        ":death-server",
    ],
    # Use a faster implementation of malloc (and show that tests pass with it).
    malloc = malloc(),
    # This test is fairly fast, but our GitHub Action Runner compiles with
    # -c dbg --config=asan (~1s per test) and runs it 100x which often hits
    # the default "short" timeout without sharding.
    shard_count = 5,
    # TODO(benh): resolve build issues on Windows and then remove
    # these 'target_compatible_with' constraints.
    target_compatible_with = select({
        "@platforms//os:linux": [],
        "@platforms//os:macos": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":death-constants",
        ":streaming",
        "//eventuals/grpc",
        "//test:promisify-for-test",
        "@bazel_tools//tools/cpp/runfiles",
        "@com_github_google_googletest//:gtest",
        "@com_github_grpc_grpc//examples/protos:helloworld_cc_grpc",
        "@com_github_grpc_grpc//examples/protos:keyvaluestore",
    ] + select(
        {
            # We support backward lib only for Linux.
            # The support for Windows and MacOS might be added in future.
            # https://github.com/3rdparty/eventuals/issues/450
            # https://github.com/3rdparty/eventuals/issues/616
            "@platforms//os:linux": [
                "@com_github_3rdparty_bazel_rules_backward_cpp_stacktrace//:backward-stacktrace",
            ],
            "//conditions:default": [],
        },
    ),
)

cc_library(
    name = "ordering-dependent-test",
    hdrs = [
        "ordering-dependent-test.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "//eventuals",
        "//eventuals/grpc",
        "@com_github_google_googletest//:gtest",
    ],
)
